package com.example.check.web.controller;

import com.example.check.entity.UserEntity;
import com.example.check.realm.service.ShiroService;
import com.example.check.service.UserService;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;

import static com.example.common.AssertThrowUtil.$;

/**
 * Created by 13296 on 2017/7/14.
 */
@Controller
public class UserController {
    @Resource
    private UserService fun;

    @Autowired
    private ShiroService shiroService;

    @RequestMapping(method = RequestMethod.GET, path = "/searchUser.do")
    public String searchUserView(String field, String code, HttpServletRequest request,
                                 Model model) throws Exception {
        ArrayList<UserEntity> userList = null;
        if (code == null) {
            userList = fun.searchAllUser();
        } else {
            userList = fun.SearchUserLikeByField(field, code);
        }
        model.addAttribute("searchUserList",userList);
//        HttpSession session = request.getSession();
//        session.setAttribute("userList",userList);
        return "searchUser";
    }

    @RequestMapping(method = RequestMethod.GET, path = "/deleteUser.do")
    public String deleteUserView(int id, HttpServletRequest request) throws Exception {
        ArrayList<UserEntity> userList = null;

        fun.deleteUserById(id);
        userList = fun.searchAllUser();
        HttpSession session = request.getSession();
        session.setAttribute("userList",userList);
        request.setAttribute("message","删除用户成功！");
        return "manageUser";
    }

    @RequestMapping(method = RequestMethod.POST, path = "/editUser.do")
    public String editUser(int id, String account, String name, String roleLevel, String sex, String telephone, String email, String address, HttpServletRequest request) {
        try {
            account = $("账户不能为空！", account);
            name = $("名字不能为空！", name);

            UserEntity u = new UserEntity();
            u.setId(id);
            u.setAccount(account);
            u.setName(name);
            u.setRoleLevel(roleLevel);
            u.setSex(sex);
            u.setTelephone(telephone);
            u.setEmail(email);
            u.setAddress(address);

            fun.editUser(u);

        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            ArrayList<UserEntity> userList = fun.searchAllUser();
            HttpSession session = request.getSession();
            session.setAttribute("userList",userList);
        } catch (Exception e) {
            e.printStackTrace();
        }

        request.setAttribute("message","修改用户成功！");
        return "manageUser";
    }

    @RequestMapping(method = RequestMethod.GET, path = "/editUserView.do")
    public String editUserView(int id, HttpServletRequest request) {
        try {
            UserEntity chosenUser = fun.SearchUserByID(id);
            HttpSession session = request.getSession();
            session.setAttribute("chosenUser",chosenUser);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "editUser";
    }

    @RequestMapping(method = RequestMethod.GET, path = "/manageUser.do")
    public String manageUserView(String field, String code, Model model, HttpServletRequest request) throws Exception{

        ArrayList<UserEntity> userList = null;
        if (code == null) {
            userList = fun.searchAllUser();
        } else {
            userList = fun.SearchUserLikeByField(field, code);
        }

        HttpSession session = request.getSession();
        session.setAttribute("userList",userList);

        return "manageUser";
    }

    @RequestMapping(method = RequestMethod.GET, path = "/addUser.do")
    public String addUser(String account, String password, String passwordAgain, String name,
                          String sex, String telephone, String email, String address, HttpServletRequest request) {
        HttpSession session = request.getSession();
        try {
            if(!password.equals(passwordAgain)){
                throw new Exception("两次输入密码有误，请重新填写!");
            }
            String roleLevel = "2";//一般用户
            if (sex.equals("1")) {
                sex = "男";
            } else {
                sex = "女";
            }

            fun.enroll(account, password, name, roleLevel, sex, telephone, email, address);
        } catch (Exception e) {
            request.setAttribute("message", e.getMessage());
            return "addUser";
        }
        request.setAttribute("message", "创建成功！");

        try {
            ArrayList<UserEntity> userList = fun.searchAllUser();
            session.setAttribute("userList",userList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "manageUser";
    }

    @RequestMapping(method = RequestMethod.GET,path="/addUserView.do")
    public String addUserView(){

        return null;
    }

    @RequestMapping(method = RequestMethod.GET,path="/giveUserRole.do")
    public String giveUserRole(int id,String role,  HttpServletRequest request){
        try {
            role = $("账户不能为空！", role);
            fun.giveUserRole(id,role);

            ArrayList<UserEntity> userList = fun.searchAllUser();
            HttpSession session = request.getSession();
            session.setAttribute("userList",userList);
        }catch (Exception e) {
            e.printStackTrace();
        }
        request.setAttribute("message","授予角色成功！");
        return "manageUser";
    }


    @RequestMapping(method = RequestMethod.GET,path="/giveUserRoleView.do")
    public String giveUserRoleView(int id, HttpServletRequest request){
        try {
            UserEntity chosenUser = fun.SearchUserByID(id);
            HttpSession session = request.getSession();
            session.setAttribute("chosenUser",chosenUser);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "giveUserRole";
    }

    @RequestMapping(method = RequestMethod.GET,path = "/editMyself.do")
    public String editMyselfView(){

        return "editMyself";
    }

    @RequestMapping(method = RequestMethod.POST,path = "/editMyself.do")
    public String editMyself(int id, String account, String name, String roleLevel, String sex, String telephone, String email, String address, HttpServletRequest request){
        try {
            account = $("账户不能为空！", account);
            name = $("名字不能为空！", name);
            UserEntity u = new UserEntity();
            u.setId(id);
            u.setAccount(account);
            u.setName(name);
            u.setRoleLevel(roleLevel);
            u.setSex(sex);
            u.setEmail(email);
            u.setAddress(address);
            u.setTelephone(telephone);

            fun.editUser(u);

            HttpSession session = request.getSession();
            ArrayList<UserEntity> userList = fun.searchAllUser();
            session.setAttribute("userList",userList);

            session.setAttribute("currentUser",u);
        } catch (Exception e) {
            e.printStackTrace();
        }
        request.setAttribute("message","修改资料成功！");
        return "firstPage";
    }

}
